package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.InviteuserAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.InviteuserBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.Userdata;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class InviteuserActivity extends BaseActivity {
    private ImageView icon_back;
    private String token;
    private RecyclerView rc_inviteuser;
    private InviteuserAdapter adapter;
    private int type;
    private EditText editTextTextPersonName;
    private ArrayList<Userdata> adapterData = new ArrayList<>();
    private ArrayList<Userdata> data = new ArrayList<>();

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(view -> finish());
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_inviteuser);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        showLoading();
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        icon_back = findViewById(R.id.icon_back);
        rc_inviteuser = findViewById(R.id.rc_inviteuser);
        type = getIntent().getIntExtra("type", 0);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(InviteuserActivity.this);
        rc_inviteuser.setLayoutManager(linearLayoutManager);
        adapter = new InviteuserAdapter(adapterData);
        rc_inviteuser.setAdapter(adapter);
        getInviteuser(token, EXTRA);
        editTextTextPersonName = findViewById(R.id.editTextTextPersonName);
        editTextTextPersonName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                adapterData.clear();
                if (s.toString().isEmpty()) {
                    adapterData.addAll(data);
                } else {
                    for (Userdata datum : data) {
                        if (datum.getName().contains(s)) {
                            adapterData.add(datum);
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void getInviteuser(String token, String extra) {
        NetUtils2.getInstance().getApiService().getInviteUser(token, extra, type, 1, 100)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr0", decryptStr);
                            Gson gson = new Gson();
                            InviteuserBean bean = gson.fromJson(decryptStr, InviteuserBean.class);
                            InviteuserActivity.this.data.addAll(bean.getData().getUsers());
                            InviteuserActivity.this.adapterData.addAll(InviteuserActivity.this.data);
                            adapter.notifyDataSetChanged();
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                    }
                });
    }
}
